<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;
use think\Exception;//事务处理

class Cotype extends Model
{
    //获取公司类型的数据相关数据
    public function getcotype($res)
    {
        $langs = $where['a.lang'] = $res['lang'];//语言标志
        $join = [
            ['nt_cotype b', 'a.mkey=b.mkey']
        ];
        $table = 'multi_lang';
        $pagelimit['limit'] = $res['pageSize'];
        $pagelimit['start'] = ($res['page'] - 1) * $res['pageSize'];
        $datares = getmultidata($table, $join, $where, $pagelimit);
        foreach ($datares['data'] as $datakey => $dataval) {
            //职位名称
            $datares['data'][$datakey]['positioname'] = $this->getposition($dataval['position_ids'], $langs);
        }
        $data['data'] = $datares['data'];
        $data['count'] = $datares['count'];
        return $data;
    }

    //获取对应语言下的职位名称
    protected function getposition($position_ids, $langs)
    {
        $positionarr = explode(',', $position_ids);
        foreach ($positionarr as $position_idskey => $position_idsval) {
            $where['mkey'] = 'position_' . $position_idsval;
            //对应下的语言名称
            $positionname[] = getlangname('multi_lang', $where, $langs);
        }
        return implode(',', array_filter($positionname));
    }

    //编辑 添加公司类型
    public function addeditcotype($data)
    {
        if ($data['act'] == 'cotypadd') {
            $ckdata = notEmpty(['cotypename' => lang("type_name")]);
        } else {
            $dname_zh = trim($data['lang_zh']);//中文
            $dname_en = trim($data['lang_en']);//英文
            $dname_pt = trim($data['lang_pt']);//葡萄牙文
            $ckdata = notEmpty(['lang_zh' => lang("Chinese_type_name"), 'lang_en' => lang("Engligh_type_name"), 'lang_pt' => lang("Portuguese_type_name")]);
        }
        if ($ckdata['status']==2) {
            return $ckdata;
        }
        if (!isset($data['position_ids'])) {
            return array('msg' => lang("job_authorized_can't_be_empty"), 'status' => 2);
        }
        //去重，去空
        $position_ids = array_unique(array_filter($data['position_ids']));
        $lang = $data['lang'];
        unset($data['lang']);
        if ($data['act'] == 'cotypadd') {
            $depalike = 'cotype';
            $langwhere['mkey'] = array('like', $depalike . '%');
            $cotypename = $langwhere['content'] = trim($data['cotypename']);
            $langzh = getables('multi_lang', $langwhere);
            if ($langzh) {
                return array('msg' => $cotypename . lang("campany_type_exists"), 'status' => 3);
            }
            //权限id的写入
            $data['position_ids'] = implode(',', $position_ids);//去重
            $data['create_at'] = $data['update_at'] = time();
            $cotypeid = $this->strict(false)->insertGetId($data);//公司类型表
            $cotypekey = 'cotype_' . $cotypeid;
            //更新数据
            $this->where('id', $cotypeid)->setField('mkey', $cotypekey);
            $langdata = getables('language');
            $multidata = array();
            foreach ($langdata as $langdatakey => $langdataval) {
                $multidata[$langdatakey]['content'] = $cotypename;
                $multidata[$langdatakey]['mkey'] = $cotypekey;
                $multidata[$langdatakey]['lang'] = $langdataval['code'];
            }
            $langid = Db::name('multi_lang')->insertAll($multidata);//语言表
            if ($cotypeid && $langid) {
                Db::commit();
                return array('msg' => $cotypename . lang("added_successfully"), 'status' => 1);
            } else {
                return array('msg' => $cotypename . lang("add_failed"), 'status' => 0);
            }
        } elseif ($data['act'] == 'cotypedit') {
            if (empty($position_ids)) {
                return array('msg' => lang("job_authorized_can't_be_empty"), 'status' => 2);
            }
            switch ($lang) {
                case 'zh':
                    $langedit = $dname_zh;
                    break;
                case 'en':
                    $langedit = $dname_en;
                    break;
                case 'pt':
                    $langedit = $dname_pt;
                    break;
                default:
                    $langedit = $dname_zh;
                    break;
            }
            $admin = session('admin');
            if ($admin['position_id'] != 1) {
                $country_id = getinfo($admin['position_id'], 'position')['country_id'];
                $where['country_id'] = $country_id;
                $positionarr = array_column(getables('position', $where), 'id');
                //不是全球管理员
                $parr = explode(',', $this->where('id', $data['id'])->value('position_ids'));//之前的权限
                foreach ($parr as $parrkey => $parrval) {
                    if (in_array($parrval, $positionarr)) {
                        unset($parr[$parrkey]);
                    }
                }
                //去重 去空 合并
                $posids = array_unique(array_filter(array_merge($parr, $position_ids)));
                $datadepart['position_ids'] = implode(",", $posids);
            } else {
                //是全球管理员
                $datadepart['position_ids'] = implode(',', $position_ids);//去重
            }
            Db::startTrans();
            $datadepart['update_at'] = time();
            //编辑更新部门表，以及更新语言表
            $resupid = $this->where('id', $data['id'])->update($datadepart);//更新公司类型表
            //更新语言表
            $departmkey = 'cotype_' . $data['id'];
            $langupidzh = Db::name('multi_lang')->where(['mkey' => $departmkey, 'lang' => 'zh'])->setField('content', $dname_zh);//更新中文公司类型
            $langupiden = Db::name('multi_lang')->where(['mkey' => $departmkey, 'lang' => 'en'])->setField('content', $dname_en);//更新英文公司类型
            $langupidpt = Db::name('multi_lang')->where(['mkey' => $departmkey, 'lang' => 'pt'])->setField('content', $dname_pt);//更新葡文公司类型
            if ($resupid !== false && $langupidzh !== false && $langupiden !== false && $langupidpt !== false) {
                Db::commit();
                return array('msg' => $langedit . lang("edited_successfully"), 'status' => 1);
            } else {
                return array('msg' => $langedit . lang("edit_failed"), 'status' => 0);
            }
        }
    }

    //删除公司类型
    public function delcotype($data)
    {
        $lang = $data['lang'];
        //查询具体语言下的部门
        $cotype_id = $data['id'];
        $cotypewhere['mkey'] = 'cotype_' . $cotype_id;
        $langname = getlangname('multi_lang', $cotypewhere, $lang);
        //公司属性中没有对应的公司类型
        $where[] = ["exp", "FIND_IN_SET($cotype_id,cotype_ids)"];
        $coattr = Db::name('coattr')->where($where)->select();
        if ($coattr) {
            return array('msg' => $langname . lang("contains_attribute_can't_be_deleted"), 'status' => 0);
        }
        //公司含有该类型无法删除
        $company_typeids = Db::name('company')->where('type_id', $data['id'])->select();
        if ($company_typeids) {
            return array('msg' => $langname . lang("contains_company_can't_bem_delete"), 'status' => 0);
        }

        Db::startTrans();
        $retcotype = $this->where('id', $data['id'])->delete();//类型表
        $langdel = Db::name('multi_lang')->where('mkey', 'cotype_' . $data['id'])->delete();
        if ($retcotype && $langdel) {
            Db::commit();
            return array('msg' => $langname . lang("deleted_successfully"), 'status' => 1);
        } else {
            return array('msg' => $langname . lang("failed_to_delete"), 'status' => 0);
        }
    }
}


